package com.android.tnovoselec.travelassistant.adapter;

import java.util.Collections;
import java.util.List;

import com.android.tnovoselec.travelassistant.Constant;
import com.android.tnovoselec.travelassistant.R;
import com.android.tnovoselec.travelassistant.dto.BankomatDto;
import com.android.tnovoselec.travelassistant.dto.BenzinskaDto;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BenzinskeAdapter extends BaseAdapter {
    private List<BenzinskaDto> items;
    private Context context;
    private LayoutInflater inflater;

    public BenzinskeAdapter(Context context) {
	super();
	this.context = context;
    }

    @Override
    public int getCount() {
	return items != null ? items.size() : 0;
    }

    @Override
    public Object getItem(int position) {
	return items.get(position);
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	View v;
	if (convertView != null) {
	    v = convertView;
	} else {
	    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    v = inflater.inflate(R.layout.bankomati_list_item, parent, false);
	}

	BenzinskaDto item = items.get(position);
	TextView title = (TextView) v.findViewById(R.id.list_item_title);
	TextView desc = (TextView) v.findViewById(R.id.list_item_description);
	ImageView logo = (ImageView) v.findViewById(R.id.list_item_logo);

	if (title != null) {
	    title.setText(item.getAdresa());
	}
	if (desc != null) {
	    desc.setText(item.getBank_id());
	}

	if (logo != null) {
	    if (item.getBank_id().toLowerCase().contains(Constant.INA_REGEX)) {
		logo.setImageResource(R.drawable.ina_icon);
	    } else if (item.getBank_id().toLowerCase().contains(Constant.PETROL_REGEX)) {
		logo.setImageResource(R.drawable.petrol_icon);
	    } else if (item.getBank_id().toLowerCase().contains(Constant.OMV_REGEX)) {
		logo.setImageResource(R.drawable.omv_icon);
	    } else if (item.getBank_id().toLowerCase().contains(Constant.LPG_REGEX)) {
		logo.setImageResource(R.drawable.lpg_icon);
	    } else if (item.getBank_id().toLowerCase().contains(Constant.CROBENZ_REGEX)) {
		logo.setImageResource(R.drawable.crobenz_icon);
	    }
	}
	return v;
    }

    public void setData(List<BenzinskaDto> items) {
	this.items = items;
	Collections.sort(items);
	notifyDataSetChanged();
    }
}
